package com.quasar.hpatient.view.friends;


import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.FriendsData;
import com.quasar.hpatient.model.medicalmodel.SelectFriendsData;
import com.quasar.hpatient.onInterface.OnFriendListener;
import com.quasar.hpatient.presenter.NewsFriendPresenter;
import com.quasar.hpatient.view.adapter.NewsFriendAdapter;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * 好友消息
 */
@EActivity(R.layout.activity_news)
public class NewsFriendActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        SwipeRefreshLayout.OnRefreshListener, OnFriendListener {

    public NewsFriendAdapter newsFriendAdapter;
    private int delayMillis = 1000;//设置刷新等待的时间
    private NewsFriendPresenter newsFriendPresenter;
    private final String ADDTO = "2";//添加
    private final String REFUSE = "1";//拒绝


    @ViewById(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;

    @ViewById(R.id.main_toolbar)
    Toolbar main_toolbar;

    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_list.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        newsFriendPresenter = new NewsFriendPresenter(this);
        initAdapter();
    }

    //初始化adapter布局
    public void initAdapter() {
        newsFriendAdapter = new NewsFriendAdapter();
        newsFriendAdapter.setOnLoadMoreListener(this, rv_list);
        newsFriendAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        rv_list.setAdapter(newsFriendAdapter);
        //初始化数据
        newsFriendPresenter.PostNewsFriend();

        //item点击
        newsFriendAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                FriendsData friendsData = newsFriendAdapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.tv_addto://添加
                        newsFriendPresenter.operationFriend(friendsData.getId() + "", ADDTO);
                        break;

                    case R.id.tv_Refuse://拒绝
                        newsFriendPresenter.operationFriend(friendsData.getId() + "", REFUSE);
                        break;
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        ViseLog.d("onRefresh");
        newsFriendPresenter.PostNewsFriend();
        newsFriendAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //下拉后添加新数据
                swipeLayout.setRefreshing(false);//下拉刷新关闭
                newsFriendAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }

    @Override
    public void onLoadMoreRequested() {
        newsFriendAdapter.loadMoreEnd();
    }


    @Override
    public void onNewsFriendSuccess(AnotherResult<FriendsData> data) {
        newsFriendAdapter.setNewData(data.getList());
    }

    @Override
    public void onNewsFriendRefreshUI(DataStateResult data) {
        if (data.getMsgCode().equals("0x00000000")) {
            msg("操作成功");
        }
        onRefresh();
    }

    @Override
    public void onSuccess(DataStateResult data) {
        //初始化数据
        newsFriendPresenter.PostNewsFriend();
    }

    @Override
    public void onError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onRefreshUI() {
        newsFriendAdapter.notifyDataSetChanged();
    }

    @Override
    public void searchFriend(SelectFriendsData selectFriendsData) {

    }

    @Override
    public void onAddtoFriend(DataStateResult dataStateResult) {

    }
}
